package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.domain.OrderSetting;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: OrderSettingServiceImpl
 * @Author: Solomon
 * @Date: 2020/7/7 16:08
 * @Description:
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    //批量导入预约设置
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                //在导入的时候，不能直接导入，需要判断当前日期是否已经进行了预约设置
                int count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());

                if (count == 0){
                    //如果没有进行预约设置，那么我们就进行新增操作
                    orderSettingMapper.add(orderSetting);
                }else {
                    //如果进行了预约设置，那么我们就进行更新操作
                    orderSettingMapper.updateNumberByOrderDate(orderSetting);
                }
            }
        }
    }

    //根据月份查询当前月的预约设置信息
    @Override
    public List<Map> findOrderSettingByMonth(String date) {
        //页面传递的月份，是2020-7这种格式的，我们有一个简单的方法：
        // 就是不管页面传递的是几月，我们都从该月的1号查到31号
        String beginDate = date + "-1";
        String endDate = date + "-31";
        //封装查询条件到map
        Map<String,String> map = new HashMap<>();
        map.put("beginDate",beginDate);
        map.put("endDate",endDate);
        //查询到List<OrderSetting>集合中
        List<OrderSetting> orderSettingList = orderSettingMapper.findOrderSettingByMonth(map);

        //转换查询结果
        List<Map> resultList = new ArrayList<>();

        if (orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Object> orderMap = new HashMap<>();
                orderMap.put("date",orderSetting.getOrderDate().getDate());
                orderMap.put("number",orderSetting.getNumber());
                orderMap.put("reservations",orderSetting.getReservations());
                resultList.add(orderMap);
            }
        }
        return resultList;
    }

    //基于日历修改预约设置信息
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //根据日期进行查询，判断当前日期是否已经进行了预约设置
        int count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());

        if (count > 0){
            //已经进行了预约设置
            orderSettingMapper.updateNumberByOrderDate(orderSetting);
        }else {
            //没有设置
            orderSettingMapper.add(orderSetting);
        }
    }
}
